<?php

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

/*后台佣金记录*/
if ($_REQUEST['act'] == 'commission_log') {
    $shop_id = $_REQUEST['shop_id'];
    $commission_log = commission_log();
    $smarty->assign('ur_here',      '佣金记录');
    $smarty->assign('commission_list',$commission_log['commission_list']);
    $smarty->assign('filter',$commission_log['filter']);
    $smarty->assign('record_count',$commission_log['record_count']);
    $smarty->assign('page_count',$commission_log['page_count']);
    $smarty->assign('full_page',1);
    $smarty->assign('sort_id', '<img src="images/sort_desc.gif">');
    $smarty->assign('sort_amount', '<img src="images/sort_desc.gif">');

    assign_query_info();
    $smarty->display('commission_log.htm');
}

/*------------------------------------------------------ */
//-- ajax返回用户列表
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query')
{
    $commission_log = commission_log();
    
    $smarty->assign('commission_list',$commission_log['commission_list']);
    $smarty->assign('filter',$commission_log['filter']);
    $smarty->assign('record_count',$commission_log['record_count']);
    $smarty->assign('page_count',$commission_log['page_count']);
    $sort_flag  = sort_flag($commission_log['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('commission_log.htm'), '', array('filter' => $commission_log['filter'], 'page_count' => $commission_log['page_count']));
}


/**
 * 佣金记录列表
 * @return array 佣金记录数组
 */
function commission_log()
{   
    $result = get_filter();
    if ($result === false) 
    {
        $ex_where = " WHERE 1 ";
        $filter['shop_id'] = empty($_REQUEST['shop_id']) ? '' : trim($_REQUEST['shop_id']);
        $filter['user_id'] = empty($_REQUEST['user_id']) ? '' : trim($_REQUEST['user_id']);
         $filter['sort_by']    = empty($_REQUEST['sort_by'])    ? 'id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC'     : trim($_REQUEST['sort_order']);

        if ($filter['shop_id']) {
            //查找到店主
            $user_id = $GLOBALS['db']->getOne('SELECT user_id FROM '.$GLOBALS['ecs']->table('goods').' WHERE goods_id = "'.$filter['shop_id'].'"');
            $ex_where .=" AND user_id = '$user_id' ";
        }
        if ($filter['user_id']) {
            $ex_where .= " AND user_id = '$filter[user_id]' ";
        }
        //记录数
        $filter['record_count'] = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('commission_log') . $ex_where);
        /* 分页大小 */
        $filter = page_and_size($filter);
        $sql = "SELECT * ".
                " FROM " . $GLOBALS['ecs']->table('commission_log') . $ex_where .
                " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
                " LIMIT " . $filter['start'] . ',' . $filter['page_size'];
        //filter
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $commission_log = $GLOBALS['db']->getAll($sql);

    //数据处理
    foreach ($commission_log as $k => $v) {
        $commission_log[$k]['user_name'] = $GLOBALS['db']->getOne('SELECT user_name FROM '.$GLOBALS['ecs']->table('users').' WHERE user_id = "'.$v['user_id'].'"');
        $commission_log[$k]['add_time_format'] = date('Y-m-d',$v['add_time']);
        switch ($v['yj_type']) {
            case '1':
                $commission_log[$k]['type_name'] = '用户佣金';
                break;
             case '2':
                $commission_log[$k]['type_name'] = '服务员佣金';
                break;   
            case '3':
                $commission_log[$k]['type_name'] = '商家佣金';
                break;
            case '4':
                $commission_log[$k]['type_name'] = '区域代理佣金';
                break;
            default:
                break;
        }

    }
    $arr = array('commission_list' => $commission_log, 'filter' => $filter,
        'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
    return $arr;
}

?>